package com.wll.flutter_trip;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wll.flutter.asr_plugin.AsrPlugin;
import com.wll.flutter.splash_plugin.FlutterSplashScreenPlugin;
import com.wll.flutter.splash_plugin.SplashScreen;

import java.util.Objects;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        SplashScreen.show(this,true);

        super.onCreate(savedInstanceState);

        configureFlutterEngine(Objects.requireNonNull(this.getFlutterEngine()));

//        GeneratedPluginRegistrant.registerWith(Objects.requireNonNull(this.getFlutterEngine()));
//        registerSelfPlugin(this.getFlutterEngine());

//        if (this.getFlutterView()!= null){
//            registerSelfPlugin(this.getFlutterView());
//        }
//        this.registrarFor();

    }

    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
//        GeneratedPluginRegistrant.registerWith(flutterEngine);
        registerWith(flutterEngine);
        //flutter sdk >= v1.17.0 时使用下面方法注册自定义plugin
        AsrPlugin.registerWith(this, flutterEngine.getDartExecutor().getBinaryMessenger());
        FlutterSplashScreenPlugin.registerWith(this, flutterEngine.getDartExecutor().getBinaryMessenger());
    }

    public void registerWith(@NonNull FlutterEngine flutterEngine) {
        ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(Objects.requireNonNull(this.getFlutterEngine()));
        flutterEngine.getPlugins().add(new io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin());
//        org.devio.flutter.splashscreen.FlutterSplashScreenPlugin.registerWith(shimPluginRegistry.registrarFor("org.devio.flutter.splashscreen.FlutterSplashScreenPlugin"));
        com.flutter_webview_plugin.FlutterWebviewPlugin.registerWith(shimPluginRegistry.registrarFor("com.flutter_webview_plugin.FlutterWebviewPlugin"));
        flutterEngine.getPlugins().add(new io.flutter.plugins.imagepicker.ImagePickerPlugin());
        flutterEngine.getPlugins().add(new io.flutter.plugins.pathprovider.PathProviderPlugin());
        flutterEngine.getPlugins().add(new io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin());
        flutterEngine.getPlugins().add(new com.tekartik.sqflite.SqflitePlugin());
        flutterEngine.getPlugins().add(new io.flutter.plugins.urllauncher.UrlLauncherPlugin());
    }

//    private void registerSelfPlugin(FlutterEngine flutterEngine) {
////    private void registerSelfPlugin(FlutterView flutterView) {
////        AsrPlugin.registerWith(flutterView);
//
////        PluginRegistry pluginRegistry = flutterView.getPluginRegistry();
////        PluginRegistry.Registrar registrar = pluginRegistry.registrarFor("com.wll.flutter.asr_plugin.AsrPlugin");
////        AsrPlugin.registerWith(registrar);
//
//        // shim 垫片
//        ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(flutterEngine);
//        PluginRegistry.Registrar registrar = shimPluginRegistry.registrarFor("com.wll.flutter.asr_plugin.AsrPlugin");
//        AsrPlugin.registerWith(registrar);
//    }
}
